package com.donleo.stmg.entity.service.impl;

import com.donleo.stmg.common.api.CommonResult;
import com.donleo.stmg.entity.model.Daily;
import com.donleo.stmg.entity.mapper.DailyMapper;
import com.donleo.stmg.entity.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donleo.stmg.entity.vo.DailyVo;
import com.donleo.stmg.utils.CommonPath;
import com.donleo.stmg.utils.DateUtil;
import com.donleo.stmg.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * <p>
 * 日报表 服务实现类
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private DailyMapper dailyMapper;

    @Override
    public CommonResult add(Daily daily) {
        daily.setStatus(CommonPath.SUBMITTED.getValue());
        daily.setCommitTime(new Date());
        int i;
        if (daily.getId() == null) {
            i = dailyMapper.insert(daily);
        } else {
            i = dailyMapper.updateById(daily);
        }
        if (i == 1) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult saveDaily(Daily daily) {
        daily.setStatus(CommonPath.DRAFT.getValue());
        int i;
        if (daily.getId() == null) {
            i = dailyMapper.insert(daily);
        } else {
            i = dailyMapper.updateById(daily);
        }
        if (i == 1) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult deletes(List<Integer> asList) {
        int i = dailyMapper.deleteBatchIds(asList);
        if (i > 0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult updateDaily(Daily daily) {
        int i = dailyMapper.updateById(daily);
        if (i == 1) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public Daily findById(Integer id) {
        return dailyMapper.selectById(id);
    }

    @Override
    public List<Daily> findAll(DailyVo dailyVo) {
        return dailyMapper.findAll(dailyVo);
    }

    @Override
    public long getCount(DailyVo dailyVo) {
        return dailyMapper.getCount(dailyVo);
    }

    @Override
    public CommonResult showed(Integer id) {
        int i = dailyMapper.updateStatusShowed(id);
        if (i == 1) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public String exportWord(Daily daily) {
        //模板路径
        String inputUrl = CommonPath.WORD_TEMPLATE.getValue();
        //生成文件名字
        String wordName = daily.getUsername() + DateUtil.getWordCurrent() + ".docx";
        //生成新word路径
        String outputUrl = CommonPath.WORD_DAILY.getValue() + wordName;
        Map<String, String> tempMap = new HashMap<>();
        tempMap.put("username", daily.getUsername());
        tempMap.put("dailyTime", DateUtil.getStringDate(daily.getDailyTime()));
        tempMap.put("coach", daily.getCoach());
        tempMap.put("headMaster", daily.getHeadMaster());
        tempMap.put("title", daily.getTitle());
        tempMap.put("studyContent", daily.getStudyContent());
        tempMap.put("harvest", daily.getHarvest());
        tempMap.put("shortage", daily.getShortage());
        List<String[]> sList = new ArrayList<String[]>();
        boolean b = WordUtil.changWord(inputUrl, outputUrl, tempMap, sList);
        if (b) {
            return wordName;
        }
        return null;
    }

    @Override
    public void downLoad(String fileName, HttpServletResponse response) {
        File file = new File(CommonPath.WORD_DAILY.getValue() + fileName);
        try {
            if (file.exists()) {
                byte[] data = FileCopyUtils.copyToByteArray(file);
                ServletOutputStream outputStream = response.getOutputStream();
                response.reset();
                response.setContentType("application/octet-stream");
                response.setCharacterEncoding("UTF-8");
                response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                response.setHeader("Access-Control-Allow-Origin","*");
                response.setHeader("Access-Control-Allow-Methods","Post");
                response.setHeader("Access-Control-Allow-Headers",":x-requested-with,content-type");
                outputStream.write(data);
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
